package com.linkage.educloud.ah.view.activity.app;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.SubjectTypeAdapter;
import com.linkage.educloud.ah.bean.StudyTypeBean;
import com.linkage.educloud.ah.bean.SubjectBean;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.linkage.educloud.ah.widget.NoScrollGridView;
import com.zzhou.common.library.util.LogUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/12/19.
 */

public class SubjectTypeActivity extends BasicTitleActivity {

    @BindView(R.id.ac_subjecttype_grade)
    NoScrollGridView ac_subjecttype_grade;
    @BindView(R.id.ac_subjecttype_subject)
    NoScrollGridView ac_subjecttype_subject;
    @BindView(R.id.ac_subjecttype_commit)
    Button ac_subjecttype_commit;

    private static final String KEY_LIST = "lsit";

    private List<StudyTypeBean> mList = new ArrayList<>();

    private SubjectTypeAdapter gradeAdapter;
    private SubjectTypeAdapter subjectAdapter;

    private int grade_position = 0;
    private int subject_position = 0;

    public static void launch(Context context, List<StudyTypeBean> list) {
        Intent intent = new Intent(context, SubjectTypeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_LIST, (Serializable) list);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_subject_type;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("全部分类");
        initData();
    }

    private void initData() {
        mList = (List<StudyTypeBean>) getIntent().getSerializableExtra(KEY_LIST);
        // 根据效果图需要，将gridview最后一行填满
        for (int a = 0; a < mList.size(); a++) {
            List<SubjectBean> list = mList.get(a).getSubjects();
            while (list.size() % 4 != 0) {
                SubjectBean bean = new SubjectBean();
                bean.setId(-1);
                bean.setName("");
                list.add(bean);
            }
        }
        while (mList.size() % 4 != 0) {
            StudyTypeBean bean = new StudyTypeBean();
            bean.setId(-1);
            bean.setName("");
            mList.add(bean);
        }

        gradeAdapter = new SubjectTypeAdapter(this, mList, -1);
        ac_subjecttype_grade.setAdapter(gradeAdapter);
        subjectAdapter = new SubjectTypeAdapter(this, mList, 0);//默认展开第一个
        ac_subjecttype_subject.setAdapter(subjectAdapter);

        ac_subjecttype_grade.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gradeAdapter.changeSelected(position);
                subjectAdapter.changeBottom(position);

                grade_position = position;
            }
        });

        ac_subjecttype_subject.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                subjectAdapter.changeSelected(position);

                subject_position = position;
            }
        });

        ac_subjecttype_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("grade === " + mList.get(grade_position).getName());
                LogUtils.e("subject === " + mList.get(grade_position).getSubjects().get(subject_position).getName());
            }
        });
    }
}